package com.bonc.modules.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.bonc.common.utils.*;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.business.entity.TbCommonParamInfoEntity;
import com.bonc.modules.business.entity.TbWorkOrderInfoEntity;
import com.bonc.modules.business.service.TbWorkOrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bonc.modules.business.dao.TbWorkOrderUrgeDao;
import com.bonc.modules.business.entity.TbWorkOrderUrgeEntity;
import com.bonc.modules.business.service.TbWorkOrderUrgeService;


@DataSource("data")
@Service("tbWorkOrderUrgeService")
public class TbWorkOrderUrgeServiceImpl extends ServiceImpl<TbWorkOrderUrgeDao, TbWorkOrderUrgeEntity> implements TbWorkOrderUrgeService {

    @Autowired
    private TbWorkOrderUrgeService tbWorkOrderUrgeService;

    @Autowired
    private TbWorkOrderInfoService tbWorkOrderInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String identifier = (String)params.get("identifier");
        IPage<TbWorkOrderUrgeEntity> page = this.page(
                new Query<TbWorkOrderUrgeEntity>().getPage(params),
                new QueryWrapper<TbWorkOrderUrgeEntity>().like(StringUtils.isNotBlank(identifier),"identifier", identifier)
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(TbWorkOrderUrgeEntity tbWorkOrderUrge) {
        return SqlHelper.retBool(this.getBaseMapper().insert(tbWorkOrderUrge));
    }

    @Override
    public List<Map<String, Object>> queryUrgeList() {
        return this.baseMapper.queryUrgeList();
    }

    @Override
    public AtomicReference<String> postOrderUrgeService(TbWorkOrderUrgeEntity orderUrge) {
        /*LambdaQueryWrapper<TbWorkOrderUrgeEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<TbWorkOrderUrgeEntity> list = tbWorkOrderUrgeService.list(queryWrapper);*/

        AtomicReference<String> res = new AtomicReference<>("");
        AtomicReference<String> req = new AtomicReference<>("");

        /*list.forEach(orderUrge->{

        });*/

        String url = "http://10.254.30.252:3900/api/v2/sync/CSVC/UrgeCSS";
        SupermanUtil util = new SupermanUtil();

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        orderUrge.setUrgeTime(dateFormat.format(calendar.getTime()));

        JSONObject content = new JSONObject();
        content.put("Identifier", orderUrge.getIdentifier());
        content.put("UrgeTime", orderUrge.getUrgeTime());
        content.put("UrgeReason", orderUrge.getUrgeReason());

        Map<String, Object> param = ResolveParamUtils.getAllParam(content);

        req.set(JSON.toJSONString(param));
        res.set(util.sendPost(url, req.get()));

        TbWorkOrderInfoEntity tbWorkOrderInfo = new TbWorkOrderInfoEntity();
        tbWorkOrderInfo.setIdentifier(orderUrge.getIdentifier());
        tbWorkOrderInfo.setState("06");//将工单信息表的工单状态修改为催办
        tbWorkOrderInfoService.updateById(tbWorkOrderInfo);

        //保存当条催办信息 判断是否存在
        tbWorkOrderUrgeService.save(orderUrge);

        return res;
    }

    @Override
    public ReplyCommon UrgeCSS(TbCommonParamInfoEntity tbCommonParamValue) {
        String content = "";
        String rspCode = "";
        String rspDesc = "";

        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat dateFormat1= new SimpleDateFormat("yyyyMMddHHmmss");
        String cutOffDay = dateFormat.format(calendar.getTime());
        String handingTime = dateFormat1.format(calendar.getTime());
        String transIDO = handingTime + System.currentTimeMillis();
        String providePartyID = "CSVB3510";

        /*String signjy = ResolveParamUtils.analysisAllParam(tbCommonParamValue);
        String sign = tbCommonParamValue.getSign();
        if(sign == signjy || sign.equals(signjy)){

        }else{
            rspCode = "20207";
            rspDesc = "非法的签名";
        }*/
        content = tbCommonParamValue.getContent();
        TbWorkOrderUrgeEntity tbWorkOrderUrge = JSONObject.parseObject(content,TbWorkOrderUrgeEntity.class);
        if(StringUtils.isBlank(tbWorkOrderUrge.getIdentifier()) || StringUtils.isBlank(tbWorkOrderUrge.getUrgeTime())){
            rspCode = "20101";
            rspDesc = "缺少必须的参数";
        }else{
            //通过工单编号 判断该工单是否存在  不存在返回报错信息  存在进行下一步操作
            TbWorkOrderInfoEntity tbWorkOrderInfo = tbWorkOrderInfoService.getById(tbWorkOrderUrge.getIdentifier());
            if(tbWorkOrderInfo == null){
                rspCode = "20000";
                rspDesc = "此工单不存在，请检查！";
            }else{
                tbWorkOrderInfo.setState("06");
                tbWorkOrderInfo.setHandingStatus("0");
                tbWorkOrderInfoService.updateById(tbWorkOrderInfo);
                boolean num = tbWorkOrderUrgeService.save(tbWorkOrderUrge);//将工单催办信息存入工单催办码表
                if(num == true){
                    rspCode = "00000";
                    rspDesc = "success";
                }else{
                    rspCode = "20000";
                    rspDesc = "基础服务系统内部错误";
                }
            }
        }
        return ReplyCommon.ok(transIDO,cutOffDay,providePartyID,rspCode,rspDesc);
    }

    @Override
    public TbWorkOrderUrgeEntity getById(String id) {
        return this.getBaseMapper().selectById(id);
    }
}
